package com.tianyi.controller.comUser;

import com.github.pagehelper.PageInfo;
import com.tianyi.comUser.ComUserService;
import com.tianyi.controller.BaseController;
import com.tianyi.lib.bo.PullDownBO;
import com.tianyi.lib.enums.ComUserStatus;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.AuditUserReq;
import com.tianyi.lib.req.BaseReq;
import com.tianyi.lib.req.UserAuditInfoReq;
import com.tianyi.lib.req.UserQueryReq;
import com.tianyi.model.SimpleLoginUser;
import com.tianyi.vo.comUser.ComUserDetailVO;
import com.tianyi.vo.comUser.ComUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 小程序用户controller
 *
 * @author cuixuhui
 * @date 2020/05/03
 */
@RestController
@RequestMapping("comUser")
public class ComUserController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ComUserController.class);

    @Autowired
    private ComUserService comUserService;

    @RequestMapping("queryComUserPage")
    public WebResult<?>  queryComUserPage(UserQueryReq req){

        SimpleLoginUser user = getUserFromRequest();

        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        WebResult<PageInfo<ComUserDetailVO>> pageInfoWebResult = comUserService.queryComUserPage(req);
        return pageInfoWebResult;
    }

//    导出 excel

    @RequestMapping("queryComUserDetailById")
    public WebResult<?>  queryComUserDetailById(BaseReq req){

        SimpleLoginUser user = getUserFromRequest();

        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return comUserService.queryComUserDetailById(req);
    }

    @RequestMapping(value = "updateComUserInfo", method = RequestMethod.POST)
    public WebResult<?>  updateComUserInfo(@RequestBody UserAuditInfoReq req){

        SimpleLoginUser user = getUserFromRequest();

        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return comUserService.updateComUserInfo(req);
    }

    @RequestMapping("deleteComUserById")
    public WebResult<?>  deleteComUserById(UserQueryReq req){

        SimpleLoginUser user = getUserFromRequest();

        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        WebResult<String> webResult = comUserService.deleteComUserById(req);
        return webResult;
    }



    @RequestMapping(value = "submitUserAuditInfo", method = RequestMethod.POST)
    public WebResult<?>  submitUserAuditInfo(UserAuditInfoReq req){

        SimpleLoginUser user = getUserFromRequest();

        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return comUserService.submitUserAuditInfo(req);
    }

    /**
     * 用户上传照片api
     * @param req
     * @return
     */
    @RequestMapping(value = "userUploadBuCardPhoto", method = RequestMethod.POST)
    public WebResult<?>  userUploadBuCardPhoto(UserAuditInfoReq req){

        SimpleLoginUser user = getUserFromRequest();

        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        return comUserService.userUploadBuCardPhoto(req);
    }

    @RequestMapping(value = "auditUserInfo", method = RequestMethod.GET)
    public WebResult<?>  auditUserInfo(AuditUserReq req){

        SimpleLoginUser user = getUserFromRequest();

        if (Objects.isNull(user)){
            return WebResult.error("please login");
        }
        req.setEmpId(user.getEmpId());
        req.setLoginName(user.getUserName());
        return comUserService.auditUserInfo(req);
    }

    @RequestMapping(value = "auditStatusPulldown", method = RequestMethod.GET)
    public WebResult<?>  auditStatusPulldown(){
        List<ComUserStatus> comUserStatuses = Arrays.asList(ComUserStatus.values());

        List<PullDownBO> collect = comUserStatuses.stream().map(ret -> {
            PullDownBO bo = new PullDownBO();
            bo.setLabel(ret.getValue());
            bo.setValue(ret.name());
            return bo;
        }).collect(Collectors.toList());

        return WebResult.ok(collect);
    }
}

